package com.question.dao.daoImpl;

import java.sql.SQLException;
import java.util.List;
import java.util.Map;

import org.apache.commons.dbutils.QueryRunner;
import org.apache.commons.dbutils.handlers.BeanHandler;
import org.apache.commons.dbutils.handlers.BeanListHandler;

import cn.itcast.jdbc.TxQueryRunner;

import com.question.dao.TopicDao;
import com.question.dao.daoFactory.DaoFactory;
import com.question.domain.Topic;
import com.question.utils.MyProperties;

public class TopicDaoImpl implements TopicDao {
		private QueryRunner qr = new TxQueryRunner();
		//用于获取配置信息
		MyProperties properties = new MyProperties();

		
		public Topic getATopic(int index) {
			try {
				String sql = " SELECT * FROM topic WHERE questionId = ?";
				Object[] param = {index};
				return qr.query(sql, new BeanHandler<Topic>(Topic.class),param);
			} catch (SQLException e) {
				throw new RuntimeException(e);
			}
		}
		
		
		@Override
		public List getTopic() {
			try {
				String sql = "SELECT * FROM topic";
				return qr.query(sql, new BeanListHandler<Topic>(Topic.class));
			} catch (SQLException e) {
				throw new RuntimeException(e);
			}
		}
	
		@Override
		public int setTopic(Topic t, int i) {
			try {
				String sql = "INSERT INTO topic (questionId,question,a,b,c,d,e,answer,type) VALUES (?,?,?,?,?,?,?,?,?)";
				Object[] param = {i,t.getQuestion(),t.getA(),t.getB(),t.getC(),t.getD(),t.getE(),t.getAnswer(),t.getType()};
				return qr.update(sql,param);
			} catch (SQLException e) {
				throw new RuntimeException(e);
			}
		}

}
